package com.example.seey2023.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.seey2023.entity.HealthClock;
import com.example.seey2023.entity.NcovNews;
import com.example.seey2023.utils.IPUtil;
import com.example.seey2023.vo.HealthClockVo;
import com.example.seey2023.service.HealthClockService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.seey2023.vo.DataView;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class HealthClockController {

    @Autowired
    private HealthClockService healthClockService;

    // 跳转面
    @RequestMapping("/toHealthClock")
    public String toHealthClock() {
        return "admin/healthclock";
    }

    @RequestMapping("/toHealthCard")
    public String toHealthCard() {
        return "admin/healthcard";
    }

    @RequestMapping("/toHeSuanReport")
    public String toHeSuanReport() {
        return "admin/hesuanreport";
    }

    @RequestMapping("/toVaccineReport")
    public String toVaccineReport() { return  "admin/vaccinereport";}


    @RequestMapping("/getCity")
    @ResponseBody
    public String getCity() {
        String city = IPUtil.getV4IP3();
        return city;
    }


    /**
     * 查询所有打卡记录 带有模糊查询条件 带有分页
     *
     * @param healthClockVo
     * @return
     */
    @RequestMapping("/listHealthClock")
    @ResponseBody
    public DataView listHealthClock(HealthClockVo healthClockVo) {
        // 查询所有带有模糊查询条件 带有分页
        IPage<HealthClock> page = new Page<>(healthClockVo.getPage(), healthClockVo.getLimit());
        QueryWrapper<HealthClock> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(healthClockVo.getUsername() != null, "username", healthClockVo.getUsername());
        queryWrapper.like(healthClockVo.getPhone() != null, "phone", healthClockVo.getPhone());
        //queryWrapper.eq(healthClockVo.getPhone() != null, "phone",healthClockVo.getPhone());
        healthClockService.page(page, queryWrapper);
        return new DataView(page.getTotal(), page.getRecords());
    }

    /**
     * 添加或者修改健康打卡记录数据
     */
    @RequestMapping("/addHealthClock")
    @ResponseBody
    public DataView addHealthClock(HealthClock healthClock) {
        boolean b = healthClockService.saveOrUpdate(healthClock);
        DataView dataView = new DataView();
        if (b) {
            dataView.setCode(200);
            dataView.setMsg("添加/修改成功！");
            return dataView;
        }
        dataView.setCode(100);
        dataView.setMsg("添加/修改失败");
        return dataView;
    }

    /**
     * 删除数据成功
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteHealthClockById")
    @ResponseBody
    public DataView deleteHealthClockById(Integer id) {
        healthClockService.removeById(id);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("删除数据成功！");
        return dataView;
    }

    /**
     * excel导入数据
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/excelImportHealthClock")
    @ResponseBody
    public DataView excelImportHealthClock(@RequestParam("file") MultipartFile file) throws IOException {
        DataView dataView = new DataView();
        // 1.文件不为空
        if (file.isEmpty()) {
            dataView.setMsg("文件为空，不能上传！");
        }
        // 2.POI获取Excel解析数据
        XSSFWorkbook wb = new XSSFWorkbook(file.getInputStream());
        XSSFSheet sheet = wb.getSheetAt(0);
        // 3.定义一个程序集合 接受 文件中的数据
        List<HealthClock> list = new ArrayList<>();
        XSSFRow row = null;
        // 4.解析数据，装到集合里面
        for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
            // 4.1 定义实体
            HealthClock healthClock = new HealthClock();
            // 4.2 每一行的数据，放到实体类里面
            row = sheet.getRow(i);
            // 4.3 解析数据
            healthClock.setUsername(row.getCell(0).getStringCellValue());
            healthClock.setSex(row.getCell(1).getStringCellValue());
            healthClock.setAge((int) row.getCell(2).getNumericCellValue());

            XSSFCell cell3 = row.getCell(3);
            cell3.setCellType(CellType.STRING);
            healthClock.setPhone(cell3.getStringCellValue());
//            healthClock.setPhone(row.getCell(3).getStringCellValue());

            XSSFCell cell4 = row.getCell(4);
            cell4.setCellType(CellType.STRING);
            healthClock.setMorningTemp(cell4.getStringCellValue());
//            healthClock.setMorningTemp(row.getCell(4).getStringCellValue());

            XSSFCell cell5 = row.getCell(5);
            cell5.setCellType(CellType.STRING);
            healthClock.setAfternoonTemp(cell5.getStringCellValue());
//            healthClock.setAfternoonTemp(row.getCell(5).getStringCellValue());

            XSSFCell cell6 = row.getCell(6);
            cell6.setCellType(CellType.STRING);
            healthClock.setNightTemp(cell6.getStringCellValue());
//            healthClock.setNightTemp(row.getCell(6).getStringCellValue());


            healthClock.setFeverAndCough(row.getCell(7).getStringCellValue());
            healthClock.setRecentHome(row.getCell(8).getStringCellValue());
            healthClock.setRiskZone(row.getCell(9).getStringCellValue());
            healthClock.setRecentZone(row.getCell(10).getStringCellValue());
            healthClock.setHealthStatus(row.getCell(11).getStringCellValue());
            healthClock.setCreateTime(row.getCell(12).getDateCellValue());
            // 5.添加list集合
            list.add(healthClock);
            System.out.println(list);
        }
        // 6. 插入数据库
        healthClockService.saveBatch(list);
        dataView.setCode(200);
        dataView.setMsg("excel插入成功！");
        return dataView;
    }
}
